import { message } from 'antd';
let comeFromUrl = '/';

export function render(oldRender: () => void) {
  // 1. 渲染之前做权限校验
  // fetch('/api/auth').then(item => {
  //   if (item) {
  //     oldRender()
  //   } else {
  //     history.push('/login');
  //     oldRender()
  //   }
  // });
  oldRender();
}

// @ts-ignore
export function onRouteChange({ location }) {
  // 2. 做埋点统计
  const { pathname } = location || {};
  // console.log('==========', pathname);
  // @ts-ignore
  let _hmt = _hmt || [];
  if (document.getElementById('hmjs')) {
    // @ts-ignore
    document.getElementById('hmjs').remove();
  }
  if (document.getElementById('bpjs')) {
    // @ts-ignore
    document.getElementById('bpjs').remove();
  }

  let hm = document.createElement('script');
  hm.id = 'hmjs';
  hm.src = 'https://hm.baidu.com/hm.js?6139fee3a021881788e397688eadd232';
  let hms = document.getElementsByTagName('script')[0];
  // @ts-ignore
  hms.parentNode.insertBefore(hm, hms);

  //百度推送
  let bp = document.createElement('script');
  bp.id = 'bpjs';
  let curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  } else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  let bps = document.getElementsByTagName('script')[0];
  // @ts-ignore
  bps.parentNode.insertBefore(bp, bps);

  _hmt.push(['_setCustomVar', 1, 'visiter_from', comeFromUrl, 3]);
  comeFromUrl = pathname;
  _hmt.push(['_setCustomVar', 2, 'visiter_to', comeFromUrl, 3]);
}

// @ts-ignore
import { createLogger } from 'redux-logger';
export const dva = {
  config: {
    onAction: createLogger(),
    onError(e: Error) {
      message.error(e.message, 3);
    },
  },
};
